package com.qf.smsplatform.strategy.strategy.impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.OperatorConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.common.util.CheckMobileOperatorUtil;
import com.qf.smsplatform.strategy.client.CacheClient;
import com.qf.smsplatform.strategy.strategy.Strategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * 手机号号段补全策略
 * @author zjw
 */
@Service(value = "phase")
@Slf4j
public class PhaseStrategyImpl implements Strategy {

    // 省市未知填充0
    private final int UNKNOWN_PROVINCE_CITY = 0;

    @Autowired
    private CacheClient cacheClient;

    @Override
    public boolean execute(StandardSubmit submit) {
        log.info("【策略模块-号段补齐】  开始执行~~~~~~~~~~~~");
        //======================1. 查询手机号运营商==============================
        String mobile = submit.getDestMobile();
        if (CheckMobileOperatorUtil.isChinaMobilePhoneNum(mobile)) {
            // 移动
            submit.setOperatorId(OperatorConstants.MOBILE_PHONE);
        } else if (CheckMobileOperatorUtil.isChinaUnicomPhoneNum(mobile)) {
            // 联通
            submit.setOperatorId(OperatorConstants.UNICOM_PHONE);
        }else if(CheckMobileOperatorUtil.isChinaTelecomPhoneNum(mobile)){
            // 电信
            submit.setOperatorId(OperatorConstants.TELECOM_PHONE);
        }else{
            // 未知
            submit.setOperatorId(OperatorConstants.UNKNOWN);
        }

        //======================2. 查询手机号归属地==============================
        //2.1 截取出手机号的前7位
        String phase = mobile.substring(0, 7);

        //2.2 封装key
        String key = CacheConstants.CACHE_PREFIX_PHASE + phase;

        //2.3 通过cacheClient查询到value
        String provinceAndCity = cacheClient.getObject(key);

        //2.4 判断不为空！
        if(StringUtils.isEmpty(provinceAndCity)){
            submit.setProvinceId(UNKNOWN_PROVINCE_CITY);
            submit.setCityId(UNKNOWN_PROVINCE_CITY);
        }else {
            //2.6 采用,分割，0索引位置是省，1索引位置是市
            String[] provinceCity = provinceAndCity.split(",");
            if(provinceCity.length == 2){
                submit.setProvinceId(Integer.valueOf(provinceCity[0]));
                submit.setCityId(Integer.valueOf(provinceCity[1]));
            }else{
                submit.setProvinceId(UNKNOWN_PROVINCE_CITY);
                submit.setCityId(UNKNOWN_PROVINCE_CITY);
            }
        }

        // ===================3. 匹配上当前手机短信需要采用哪个通道发送===========================
        //3.1 调用缓存模块，根据CHANNEL: + ClientID 查询当前客户绑定的所有通道
        Set<Map> channels = cacheClient.smembers(CacheConstants.CACHE_PREFIX_SMS_CHANNEL + submit.getClientID());

        //3.2 根据手机号的运营商匹配通道中的channelType -- 如果没有匹配上，选择全网通
        int operatorId = submit.getOperatorId();
        if(channels != null && channels.size() > 0){
            for (Map channel : channels) {
                // 手机号的运营商匹配通道的channelType
                int channelType = (Integer) channel.get("channelType");
                if(channelType == 0){
                    // 全网通
                    //3.3 封装gatewayID，srcNumber，fee
                    setChannelData(channel,submit);
                    continue;
                }
                if(operatorId == channelType){
                    // 当前手机号匹配上了通道
                    //3.3 封装gatewayID，srcNumber，fee
                    setChannelData(channel,submit);
                    break;
                }
            }
        }
        log.info("【策略模块-号段补齐】  执行成功！！！！！！");
        return true;
    }

    /**
     * 选择好了指定的通道，赋值
     * @param channel
     * @param submit
     */
    private void setChannelData(Map channel, StandardSubmit submit) {
        // 封装gatewayID，srcNumber，fee
        submit.setGatewayID((Integer) channel.get("channelId"));
        String srcNumber = channel.get("spNumber") + "" + channel.get("extendNumber");
        submit.setSrcNumber(srcNumber);
        submit.setFee(Long.parseLong(channel.get("price") + ""));
    }



}
